import Cookies from 'js-cookie'
import { decrypt, encrypt } from './jsencrypt'
// import { sha256 } from 'js-sha256'
// import { date } from 'jszip/lib/defaults'
import md5 from 'js-md5'

export const TokenKey = 'access_token'
const NoKey = 'no'
const TimeKey = 'expires_in'
const SecretKey = 'secret_key'

export function getToken() {
  return decrypt(Cookies.get(TokenKey))
}

export function setToken(token) {
  return Cookies.set(TokenKey, encrypt(token))
}

export function removeToken() {
  Cookies.remove(NoKey)
  Cookies.remove(TokenKey)
  Cookies.remove(TimeKey)
  Cookies.remove(SecretKey)
}

export function setLoginData(data) {
  Cookies.set(NoKey, encrypt(data[NoKey]))
  Cookies.set(TokenKey, encrypt(data[TokenKey]))
  Cookies.set(TimeKey, encrypt(data[TimeKey]))
  Cookies.set(SecretKey, encrypt(data[SecretKey]))
}

// header参数
function getParam(key) {
  return decrypt(Cookies.get(key))
}

// ISO-8601时间
export function getISOTime(date) {
  const time = date
  time.setHours(time.getHours(), time.getMinutes() - time.getTimezoneOffset())
  const isoTime = time.toISOString()
  return isoTime
}

// 时间戳
function getTimestamp(date) {
  const timestamp = parseInt(date.getTime() / 1000) + ''
  return timestamp
}

function genRandom(min, max) {
  const randomFlag = true
  const arr = [
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'q',
    'r',
    's',
    't',
    'u',
    'v',
    'w',
    'x',
    'y',
    'z',
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z'
  ]
  let str = ''
  let range = min

  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min
  }
  for (var i = 0; i < range; i++) {
    const pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[pos]
  }
  return str
}

// 签名
export function getAuthorization(date) {
  const prefix = 'wp-auth-v1'
  const no = getParam(NoKey)
  const rand = genRandom(2, 20)
  const time = getTimestamp(date)
  const token = getParam(TokenKey)
  // const member = getParam(memberType)

  // 生成sign规则
  const strParams = `no=${no}rand=${rand}time=${time}token=${token}`
  // const key = getParam(SecretKey)
  const sign = md5(strParams)

  // 结果
  const auth = prefix + '/' + no + '/' + 'web' + '/' + rand + '/' + sign
  return auth
}

//计算sign，仅demo使用，请您一定要请求后端接口获取sign
export function getSign(params) {
  var appSecret = '8ee343d1a0aa4506ab5ed762e3767e5a'
  var paramsStr = ''
  // params.forEach((val, key) => {
  //   paramsStr += val + '' + key
  // })
  for (let key in params) {
    console.log(key) // name age
    console.log(params[key]) // cookie 18
  }
  // $.each(params, function (key, val) {
  //   paramsStr += key + '' + val
  // })
  var finalStr = appSecret + paramsStr + appSecret
  return md5(finalStr).toUpperCase()
}
